#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import time
import os
import json
import ping

f = open("/opt/app/open-falcon/agent/config/cfg.json")
agentInfo = json.load(f).get('hostname')
if not  agentInfo:
     agentInfo=os.uname()[1]

def get_check():

    response = ping.quiet_ping('10.174.38.38', timeout=2, count=10, psize=64)
    if response[0] == 100:
        status = 0
    else:
        status = response[1]  
    return status


def create_record(value):
    record = {}
    record['Metric']      = "huawei.net"
    record['Endpoint']    = agentInfo
    record['Timestamp']   = int(time.time())
    record['Step']        = 30
    record['Value']       = value
    record['CounterType'] = 'GAUGE'
    record['TAGS']        = ''
    return record

if __name__ == '__main__':
    data=[]
    value = get_check()
    data.append(create_record(value))
    print json.dumps(data)
